package com.gr

import grails.test.mixin.*
import org.junit.*

import com.gr.Division;

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Division)
class DivisionTests {

	def division
	def company = new Company(title: 'duinc', email: 'duinc@gmail.com', startDate: new Date("2001/1/18"))

	@Before
	void setUp() {
		// Make sure we can invoke validate() on our User domain object.
		mockForConstraintsTests(Division)
		// Set up default division, so we can easily test single properties.
		division = new Division(title: 'divA', email: 'diva@gmail.com', startDate: new Date("2002/3/28"), companyTo: company)
	}

	/**
	 * 	title(unique: true)
	 */
	void testConstraintsTitleUnique() {

		def test = new Division(title: 'divA', email: 'somediv@gmail.com', startDate: new Date("2005/7/2"), companyTo: company)
		mockForConstraintsTests(Division, [test])

		assertFalse division.validate()
		assertEquals 'unique', division.errors['title']

		division.title = 'some'
		assertTrue division.validate()
	}

	/**
	 * title(blank: false)
	 */	
	void testConstraintsTitleBlank() {
		division.title = ''
		assertFalse division.validate()
		assertEquals 'blank', division.errors['title']

		division.title = 'notempty'
		assertTrue division.validate()
	}

	/**
	 * title(size: 2..50)
	 */
	void testConstraintsTitleSize() {
		division.title = 'd'
		assertFalse division.validate()
		assertEquals 'size', division.errors['title']

		division.title = '123456789012345678901234567890123456789012345678901'
		assertFalse division.validate()
		assertEquals 'size', division.errors['title']

		division.title = 'normalsize'
		assertTrue division.validate()
	}

	/**
	 * email(email: true)
	 */
	void testConstraintsEmailEmail() {
		division.email = 'somesting'
		assertFalse division.validate()
		assertEquals 'email', division.errors['email']

		division.email = 'valid@email.ru'
		assertTrue division.validate()
	}

	/**
	 * email(nullable: true)
	 */
	void testConstraintsEmailNullable() {
		division.email = ''
		assertTrue division.validate()
		assertNull division.errors['email']
	}

	/**
	 * startDate(nullable: true)
	 */
	void testConstraintsStartDateNullable() {
		division.startDate = null
		assertTrue division.validate()
		assertNull division.errors['startDate']
	}
	
	/**
	 * companyTo(blank: false)
	 */
	void testConstraintsCompanyToBlank() {
		division.companyTo = null
		assertFalse division.validate()
		assertEquals 'nullable', division.errors['companyTo']
		
		division.companyTo = company
		assertTrue division.validate()
	}
}
